#!/usr/local/bin/lua
--
--	Chapter 04	-	Control Structure
--
--	Michael Ge
--	2014.05.17
--

print("====================")

a = 10
if a < 0 then
	a = 0
end

--[[
if a < b then
	return a
else
	return b
end
--]]

--[[
if line > MAXLINE then
	showpage()
	line = 0
end
--]]

op = "+"
a = 1
b = 2

if op == "+" then
	r = a + b
elseif op == "-" then
	r = a - b
elseif op == "*" then
	r = a * b
elseif op == "/" then
	r = a / b
else
	error("invalid operation")
end

print("====================")

do
	a = {1, 2, 3, 4, 5,}
	local i = 1
	while a[i] do
		print(a[i])
		i = i + 1
	end
end

print("====================")

do
	repeat
		line = io.read()
	until line ~= ""
	print(line)
end

do
	x = 10
	local sqr = x / 2
	repeat
		sqr = (sqr + x / sqr) / 2
		local error = math.abs(sqr ^ 2 - x)
	until error < x / 10000
end

print("====================")

do
	for i = 10, 1, -1 do
		print(i)
	end
end

do
	for i = 1, math.huge do
		if (0.3 * i ^ 3 - 20 * i ^ 2 - 500 >= 0) then
			print(i)
			break;
		end
	end 
end

do
	i = -1
	for i = 1, 10 do
		print(i)
	end
	max = i
	print(max) 
end

do
	a = {10, 4, 8, -1, 3}
	local found = nil
	for i = 1, #a do
		if a[i] < 0 then
			found = i
			break;
		end
	end
	print(found)
end

print("====================")

do
	a = {1, 2, 3, 4, 5, 6, 7}
	for i, v in ipairs(a) do
		print(v)
	end
end

do
	t = {}
	t.x = 1
	t.y = 2
	t.z = 3
	for k in pairs(t) do
		print(k)
	end
end

do
	days = {"Sunday", "Monday", "Tuesday", "Wednesday",
			"Thursday", "Friday", "Saturday"}

	revDays = {["Sunday"] = 1, ["Monday"] = 2,
		   	   ["Tuesday"] = 3, ["Wednesday"] = 4,
		   	   ["Thursday"] = 5, ["Friday"] = 6,
		  	   ["Saturday"] = 7}

	x = "Tuesday"
	print(revDays[x])

	revDays = {}
	for k, v in pairs(days) do
		revDays[v] = k
	end
end

print("====================")